package com.kzw.oa.byxt.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.oa.byxt.model.Subject;

@SuppressWarnings("unchecked")
@Service
@Transactional
public class StatsService extends DefaultEntityManager<Subject, Long> {

	public List<Map<String, Object>> showChart() {
		
		List<Map<String, Object>> result = new ArrayList<>();
		
		// ========== 1、题目来源 ============ //
		Map<String, Object> map1 = new HashMap<>();
		map1.put("id", 1);
		map1.put("title", "题目来源");
		
		List<Map<String, Object>> dataList1 = new ArrayList<>();
		String sql1 = "SELECT d.itemValue, count(*) FROM x_subject x LEFT JOIN app_dict d on x.from_type=d.dicId GROUP BY from_type";
		List<Object[]> fromTypeList = getEntityDao().getSession().createSQLQuery(sql1).list();
		for(Object[] arr : fromTypeList) {
			Map<String, Object> sub = new HashMap<>();
			sub.put("name", arr[0]);
			sub.put("value", arr[1]);
			dataList1.add(sub);
		}
		map1.put("data", dataList1);
		result.add(map1);
		
		
		// ========== 2、题目类型 ============ //
		Map<String, Object> map2 = new HashMap<>();
		map2.put("id", 2);
		map2.put("title", "题目类型");
		
		List<Map<String, Object>> dataList2 = new ArrayList<>();
		String sql2 = "SELECT d.itemValue, count(*) FROM x_subject x LEFT JOIN app_dict d on x.type=d.dicId GROUP BY type";
		List<Object[]> typeList = getEntityDao().getSession().createSQLQuery(sql2).list();
		for(Object[] arr : typeList) {
			Map<String, Object> sub = new HashMap<>();
			sub.put("name", arr[0]);
			sub.put("value", arr[1]);
			dataList2.add(sub);
		}
		map2.put("data", dataList2);
		result.add(map2);
		
		
		// ========== 3、题目预选情况 ============ //
		Map<String, Object> map3 = new HashMap<>();
		map3.put("id", 3);
		map3.put("title", "题目预选情况");
		
		// 预选成功 + 未被预选 + 待确认 ==> 全部题目
		List<Map<String, Object>> dataList3 = new ArrayList<>();
		String hql1 = "select count(*) from Subject";
		String hql2 = "select count(*) from Subject where xscnt=0";
		String hql3 = "select count(*) from Subject where status=5";
		Long cnt1 = findUniqueByHQL(hql1);
		Long cnt2 = findUniqueByHQL(hql2);
		Long cnt3 = findUniqueByHQL(hql3);
		cnt2 = cnt2==null ? 0 : cnt2;
		cnt3 = cnt3==null ? 0 : cnt3;
		
		Map<String, Object> sub = new HashMap<>();
		sub.put("name", "预选成功");
		sub.put("value", cnt3);
		dataList3.add(sub);
		sub = new HashMap<>();
		sub.put("name", "未被预选 ");
		sub.put("value", cnt2);
		dataList3.add(sub);
		sub = new HashMap<>();
		sub.put("name", "待确认");
		sub.put("value", cnt1-cnt2-cnt3);
		dataList3.add(sub);		
		
		map3.put("data", dataList3);
		result.add(map3);
		
		
		// ========== 4、学生预选情况 ============ //
		Map<String, Object> map4 = new HashMap<>();
		map4.put("id", 4);
		map4.put("title", "学生预选情况");
		
		// 预选成功 + 未预选 + 预选失败 ==> 全部学生
		List<Map<String, Object>> dataList4 = new ArrayList<>();
		hql1 = "select count(*) from AppUser where roleType=3";
		// 共多少学生已选
		sql2 = "select count(*) from (select student_id from x_yuxuan GROUP BY student_id) as x";
		hql3 = "select count(*) from Subject where status=5";
		cnt1 = findUniqueByHQL(hql1);
		cnt3 = findUniqueByHQL(hql3);
		
		Object cnt0 = getEntityDao().getSession().createSQLQuery(sql2).uniqueResult();
		cnt2 = cnt0==null ? 0 : Long.parseLong(cnt0 + "");
		cnt3 = cnt3==null ? 0 : cnt3;
		
		sub = new HashMap<>();
		sub.put("name", "预选成功");
		sub.put("value", cnt3);
		dataList4.add(sub);
		sub = new HashMap<>();
		sub.put("name", "未预选 ");
		sub.put("value", cnt1-cnt2);
		dataList4.add(sub);
		sub = new HashMap<>();
		sub.put("name", "预选失败");
		sub.put("value", cnt2-cnt3);
		dataList4.add(sub);		
		
		map4.put("data", dataList4);
		result.add(map4);
		
		return result;
	}

	
	
}
